package com.wtwd.campus.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.dao.SysUserMapper;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.model.vo.SysUserVO;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zdl
 * @since 2021-06-10
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private RedisService redisService;
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser getCurrentLoginUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader(Constants.HEADER_TOKEN_KEY);
        Map<String, Object> userInfo = redisService.getUserInfoByToken(token);
        return JSON.parseObject((String) userInfo.get("user"), SysUser.class);
    }

    @Override
    public IPage<SysUserVO> queryPage(Page<SysUserVO> page, SysUser sysUser) {
        return sysUserMapper.querylist(page,sysUser);
    }


    @Override
    public int deleteById(Integer id) {
        return sysUserMapper.deleteById(id);
    }

    /**
     * 1.判断账号是否存在,是提示返回,否执行第二步
     * 2.保存
     *
     * @param sysUser
     * @return
     */
    @Override
    public ResultVO createUser(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysUser::getAccount, sysUser.getAccount());
        if (sysUserMapper.selectCount(queryWrapper) > 0) {
            return ResultVO.error(RespCode.USER_EXISTED);
        }
        sysUserMapper.insert(sysUser);
        return ResultVO.ok();
    }

    @Override
    public ResultVO updateUser(SysUser sysUser) {
        SysUser sysUser1 = getUserByAccount(sysUser.getAccount());
        if (sysUser1 != null && !sysUser.getUserId().equals(sysUser1.getUserId())) {
            return ResultVO.error(RespCode.USER_EXISTED);
        }
        sysUserMapper.updateById(sysUser);
        return ResultVO.ok();
    }

    @Override
    public SysUser getUserByAccount(String account) {
        if (StringUtils.isBlank(account)) return null;
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysUser::getAccount, account);
        return sysUserMapper.selectOne(queryWrapper);
    }


}
